package com.example.battle.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.battle.R;
import com.example.battle.db.BattleDao;
import com.example.battle.entity.FansName;
import com.example.battle.util.ActivityCollector;
import com.example.battle.util.ToastUtils;

import java.util.Date;

public class RegisterFansNameActivity extends AppCompatActivity {
    private EditText fansName,starName,weburl;
    private Button button;
    private ToastUtils toastUtils = new ToastUtils();
    private BattleDao battleDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_fans_name);

        fansName = findViewById(R.id.register_fansname_fansname);
        starName = findViewById(R.id.register_fansname_starname);
        weburl = findViewById(R.id.register_fansname_weburi);
        button = findViewById(R.id.button_register_fansname);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fn = fansName.getText().toString();
                String sn = starName.getText().toString();
                String wu = weburl.getText().toString();
                battleDao = Login.getInstance().getAppDB().battleDao();

                if (fn.equals("") || sn.equals("") || wu.equals("")){
                    toastUtils.showShort(RegisterFansNameActivity.this,"粉丝名或明星名或网址不能为空");
                    return;
                }

                FansName fans = battleDao.searchFansName(fn);
                if (fans != null){
                    toastUtils.showShort(RegisterFansNameActivity.this,"该粉丝名已被注册");
                    return;
                }

                fans = new FansName();
                fans.setFansName(fn);
                fans.setStarName(sn);
                fans.setStatus(0);
                fans.setWeburi(wu);
                fans.setRegisterDate(new Date().toString());
                battleDao.registerFansName(fans);
                toastUtils.showShort(RegisterFansNameActivity.this,"已发送注册申请");
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);

    }
}
